<%@ page import="com.cinsea.vote.service.VotesubjectService" %>
<%@ page import="com.cinsea.vote.service.VoteoptionService" %>
<%@ page import="com.cinsea.vote.model.Votesubject" %>
<%@ page import="com.cinsea.vote.model.Voteoption" %>
<%@ page import="com.cinsea.vote.service.VotelogService" %>
<%@ page import="org.apache.activemq.store.jdbc.JDBCAdapter" %>
<%@ page import="com.cinsea.security.service.RightService" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ include file="/common/common.jsp" %>
<%
    response.setHeader("cache-control", "no-cache");
        response.setHeader("pragma", "no-cache");
        response.setHeader("expires", "Mon 1 Jan 1990 00:00:00 GMT");
    String voteid = StringUtil.null2String(request.getParameter("id"));
    String isview = StringUtil.null2String(request.getParameter("isview"));
    VotesubjectService votesubjectService = Env.getBean("votesubjectService");
    VoteoptionService voteoptionService =Env.getBean("voteoptionService");
    RightService rightService =Env.getBean("rightService");
    LogService logService = Env.getBean("logService");
    JdbcDao jdbcDao = Env.getBean("jdbcDao");
    List researchlist=jdbcDao.queryForList("select * from ut_research where processid=?",new Object[]{voteid});
    if(researchlist.size()==0) return;
    String startdatetime=((Map)researchlist.get(0)).get("startdate")+" " +((Map)researchlist.get(0)).get("starttime");
    String enddatetime=((Map)researchlist.get(0)).get("enddate")+" " +((Map)researchlist.get(0)).get("endtime");
    String votename=(String)((Map)researchlist.get(0)).get("objname");
    if((startdatetime.compareTo(DateUtil.getCurDateTime())>0||enddatetime.compareTo(DateUtil.getCurDateTime())<0)&&!isview.equals("1")) return;
    String anonymous =StringUtil.null2String(((Map)researchlist.get(0)).get("objoption"));
    List<Votesubject> subjects = votesubjectService.find("from Votesubject where voteid=?", new Object[]{voteid});
    boolean logflag = false;
    if(logService.hasLog(voteid,Env.getRemoteUser().getId(),"86")) logflag=true;
    boolean viewflag = false;
    boolean voteflag=false;
    if(rightService.checkOpttype(voteid,1493,"","ut_research")) voteflag=true;
    if(rightService.checkOpttype(voteid,1499,"","ut_research")&&isview.equals("1")) viewflag=true;
    if(logflag&&!viewflag) {
            out.println("您已投票！");
            return;
        }
    List<Map> vcountlist = new ArrayList<Map>();
    List<Map> tcountlist = new ArrayList<Map>();
    if (logflag||(viewflag&&!voteflag)) {//投票人投票前不能查看结果，避免从众心理
        vcountlist = jdbcDao.queryForList("select distinct voteid,subjectid,optionid,count(*) as vcount from cs_votelog  group by voteid,subjectid,optionid");
        tcountlist = jdbcDao.queryForList("select distinct voteid,subjectid,count(*) as tcount from cs_votelog  group by voteid,subjectid");
    }
    if (!viewflag) pagemenustr += "addBtn(tb,'" + I18n.getText("submit") + "','S','accept',function(){onSubmit()});";
%>
<html>
<head>
    <%@ include file="/common/head.jsp" %>
    <style>
        table {
            width: 30
        }
    </style>
    <script language="javascript">
        Ext.onReady(function() {
            Ext.QuickTips.init();
            <%if(!pagemenustr.equals("")){%>
            var tb = new Ext.Toolbar();
            tb.render('pagemenubar');
            <%=pagemenustr%>
            <%if(anonymous.equals("402881ef39bd62d10139be6fcf70002c")){//允许匿名%>
                        tb.addElement('anon');
                        Ext.get('anon').setVisible(true)
                        <%}%>
            <%}%>

        });
    </script>
</head>
<body>
<!--页面菜单开始-->
<div id="divSearch">
    <form name="mainForm" id="mainForm" method="post" onsubmit="return false">
        <div id="pagemenubar"></div>
        <div id='anon' style="display: none"><input type=checkbox id='anonymous' name="anonymous" value="1" <%if(anonymous.equals("402881ef39bd62d10139be6fa88c002b")){//强制匿名%>checked<%}%> />匿名</div>
        <%
            for (Votesubject subject : subjects) {
                String subjectcontent = subject.getSubject();
                String subjectid = subject.getId();
                int bemulti = subject.getBemulti();
                int beother = subject.getBeother();
                String inputtype = "radio";
                if (bemulti == 1) inputtype = "checkbox";
                String eval = "$(\"input[type='" + inputtype + "'][name='" + subjectid + "']:checked\").length";
        %>
        <div style="margin-left: 10px;margin-top: 10px">
            <div id='divsub<%=subjectid%>' style="display: none"><%=eval%>
            </div>
            <span><%=subjectcontent%></span></br>
            <ul style="margin-left: 15px;margin-top: 10px">
                <%
                    List<Voteoption> options = voteoptionService.find("from Voteoption where subjectid=?", new Object[]{subjectid});
                    for (Voteoption voteoption : options) {
                %>
                <li style="padding: 2px">
                    <%if (!viewflag) {%>
                    <input type='<%=inputtype%>' id='<%=voteoption.getId()%>' name='<%=subjectid%>'
                           value='<%=voteoption.getId()%>'/><%=voteoption.getObjvalue()%>
                    <%
                    } else {
                        int vcount = 0;
                        int tcount = 1;
                        for (Map vcountMap : vcountlist) {
                            String currentoptionid = StringUtil.null2String(vcountMap.get("optionid"));
                            if (currentoptionid.equals(voteoption.getId()))
                                vcount = NumberUtil.getIntegerValue(vcountMap.get("vcount"), 0);
                        }
                        for (Map tcountMap : tcountlist) {
                            String currentsubjectid = StringUtil.null2String(tcountMap.get("subjectid"));
                            if (currentsubjectid.equals(subjectid))
                                tcount = NumberUtil.getIntegerValue(tcountMap.get("tcount"), 0);
                        }
                        if (tcount == 0) tcount = 1;
                        double per = MathUtil.round(vcount * 100 / tcount, 2);
                    %>
                    <%=voteoption.getObjvalue()%>
                    <div style="width: 200; height: 15px;display:inline;vertical-align: top;"
                         class="x-progress-wrap left-align">
                        <div class="x-progress-inner">
                            <div style="width: <%=per%>%; height: 15px;" id="ext-gen9" class="x-progress-bar">
                                <div style="z-index: 99; width: 100px;" id="ext-gen10"
                                     class="x-progress-text x-progress-text-back">
                                    <div style="width: 100px; height: 15px;" id="ext-gen12"><%=per%>%</div>
                                </div>
                            </div>
                        </div>
                    </div><%=vcount%>票
                    <%}%>
                </li>
                <%}%>
                <%
                    if (beother == 1) {
                %>
                <li style="padding: 2px;">
                    <%if (!viewflag) {%>
                    <input type='<%=inputtype%>' name='<%=subjectid%>'
                           value='other'/>其他:&nbsp<input id='<%="other"+subjectid%>'
                                                         name='<%="other"+subjectid%>' value=''>
                    <%
                    } else {
                        int vcount = 0;
                        int tcount = 1;
                        for (Map vcountMap : vcountlist) {
                            String currentoptionid = StringUtil.null2String(vcountMap.get("optionid"));
                            if (currentoptionid.equals("other"))
                                vcount = NumberUtil.getIntegerValue(vcountMap.get("vcount"), 0);
                        }
                        for (Map tcountMap : tcountlist) {
                            String currentsubjectid = StringUtil.null2String(tcountMap.get("subjectid"));
                            if (currentsubjectid.equals(subjectid))
                                tcount = NumberUtil.getIntegerValue(tcountMap.get("tcount"), 0);
                        }
                        if (tcount == 0) tcount = 1;
                        double per = MathUtil.round(vcount * 100 / tcount, 2);
                    %>
                    其他:&nbsp
                    <div style="width: 200; height: 15px;display:inline;vertical-align: top;"
                         class="x-progress-wrap left-align">
                        <div class="x-progress-inner">
                            <div style="width: <%=per%>%; height: 15px;" id="ext-gen9" class="x-progress-bar">
                                <div style="z-index: 99; width: 100px;" id="ext-gen10"
                                     class="x-progress-text x-progress-text-back">
                                    <div style="width: 100px; height: 15px;" id="ext-gen12"><%=per%>%</div>
                                </div>
                            </div>
                        </div>
                    </div><%=vcount%>票
                    <%}%>
                </li>
                <%}%>
            </ul>
        </div>
        <%}%>
    </form>
</div>

<SCRIPT language="javascript">

    function onSubmit() {
        var votefinished=1;
        $("div[id^='divsub']").each(function(i) {
            checked = (eval($(this).html()));
            if (checked == 0) {
                votefinished=0;
                return false;
            }
        })
        if(votefinished==0) {
            pop("投票尚未完成!");
            return;
        }
        var o = $('#mainForm').serializeArray();
        var data = {id:'<%=voteid%>',objname:'<%=votename%>'};
        for (var i = 0; i < o.length; i++) {
            if (o[i].value != null && o[i].value != "") {
                data[o[i].name] = o[i].value;
            }
        }
        var xmlrequest = $.ajax({
            data: data,
            async:true,
            type:'POST',
            url: '${ctx}/vote.do?action=vote',
            success:function(data) {
                pop("投票成功");
                window.close();
            },
            error:function(data) {

            }
        });
        //xmlrequest.responseText;

    }
    $(document).keydown(function(event) {
        if (event.keyCode == 13) {
            onSubmit();
        }
    });


</SCRIPT>
</body>
</html>